﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using MyEval;

namespace MyMoney
{
	public partial class CalInput : UserControl
	{
		public CalInput()
		{
			InitializeComponent();
		}

		private void CalInput_Resize(object sender, EventArgs e)
		{
			textBox_Input.Left = 0;
			textBox_Input.Top = 0;
			textBox_Input.Width = this.Width;
			textBox_Result.Left = 0;
			textBox_Result.Top = textBox_Input.Bottom;
			textBox_Result.Width = this.Width;
			this.Height = textBox_Input.Height + textBox_Result.Height;
		}

		decimal _value;
		public decimal Value
		{
			get
			{
				return _value;
			}
		}

		public event EventHandler ValueChanged;

		[EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible), Bindable(true)]
		override public string Text
		{
			get
			{
				return textBox_Input.Text;
			}
			set
			{
				textBox_Input.Text = value;
			}
		}

		private void textBox_Input_TextChanged(object sender, EventArgs e)
		{
			decimal t;
			try
			{
				t = CEval.Eval(textBox_Input.Text);
			}
			catch
			{
				t = 0;
			}
			if (t != _value)
			{
				_value = t;
				textBox_Result.Text = _value.ToString();
				if (ValueChanged != null) ValueChanged(this, e);
			}
		}
	}
}
